package hos.demo

import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.fragment.app.FragmentActivity
import hos.demo.ui.theme.PermissionTheme
import hos.permission.PermissionConstants
import hos.permission.PermissionX

class MainActivity : FragmentActivity() {
    // 初始化
    private val permissionX = PermissionX.init(this)

    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 权限拒绝后调用
        permissionX.onRefused {
            Log.d("request", "onRefused: $it")
        }

        setContent {
            PermissionTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Column {
                        PermissionItem("定位权限") {
                            permissionX
                                .onRefused {
                                    Log.d("request", "onRefused: $it")
                                }
                                .onRefusedDialog { delegate, permissions ->
                                    delegate.showPermissionDialog(
                                        permissions,
                                        "您已拒绝打开定位，请打开定位权限",
                                        "我明白了",
                                        "取消",
                                        false
                                    )
                                }.onRequestDialog { delegate, permissions ->
                                    delegate.showPermissionDialog(
                                        permissions,
                                        "请打开定位权限",
                                        "我明白了",
                                        "取消",
                                        true
                                    )
                                }
                                .requestLocation {
                                    Log.d("request", "requestLocation onSuccess")
                                }
                        }
                        PermissionItem("后台定位权限") {
                            permissionX
                                .clearListener()
                                .onRefused {
                                    Log.d("request", "onRefused: $it")
                                }
                                .requestLocationBackground {
                                    Log.d("request", "requestLocationBackground onSuccess")
                                }
                        }
                        ListItem(headlineText = {
                            Text(text = "系统弹窗")
                        }, modifier = Modifier.clickable {
                            permissionX
                                .clearListener()
                                .onRefusedDialog { delegate, permissions ->
                                    delegate.showPermissionDialog(
                                        permissions,
                                        "您已拒绝打开系统弹窗，请打开系统弹窗权限",
                                        "我明白了",
                                        "取消",
                                        true
                                    )
                                }
                                .requestSystemAlertWindow {
                                    Log.d("request", "requestSystemAlertWindow onSuccess")
                                }

                        })
                        ListItem(headlineText = {
                            Text(text = "通知")
                        }, modifier = Modifier.clickable {
                            permissionX.clearListener()
                                .onRefusedDialog { delegate, permissions ->
                                    delegate.showPermissionDialog(
                                        permissions,
                                        "您已拒绝打开通知，请打开通知权限",
                                        "我明白了",
                                        "取消",
                                        true
                                    )
                                }
                                .requestNotification {
                                    Log.d("request", "requestNotification onSuccess")
                                }
                        })
                        ListItem(headlineText = {
                            Text(text = "安装")
                        }, modifier = Modifier.clickable {
                            permissionX.clearListener()
                                .onRefusedDialog { delegate, permissions ->
                                    delegate.showPermissionDialog(
                                        permissions,
                                        "您已拒绝打开安装第三方应用，请打开安装第三方应用权限",
                                        "我明白了",
                                        "取消",
                                        true
                                    )
                                }
                                .requestInstallPackages {
                                    Log.d("request", "requestInstallPackages onSuccess")
                                }
                        })
                        ListItem(headlineText = {
                            Text(text = "储存权限页面")
                        }, modifier = Modifier.clickable {
                            permissionX.clearListener()
                                .onRefusedDialog { delegate, permissions ->
                                    delegate.showPermissionDialog(
                                        permissions,
                                        "您已拒绝打开存储权限，请打开存储权限",
                                        "我明白了",
                                        "取消",
                                        true
                                    )
                                }
                                .requestAllStorage {
                                    Log.d("request", "requestAllStorage onSuccess")
                                }
                        })
                        PermissionItem("相机权限") {
                            permissionX
                                .clearListener()
                                .onRefused {
                                    Log.d("request", "onRefused: $it")
                                }
                                .requestCamera {
                                    Log.d("request", "requestLocationBackground onSuccess")
                                }
                        }
                        PermissionItem("相机/存储权限") {
                            permissionX
                                .clearListener()
                                .permissions((PermissionConstants.GROUP_CAMERA + PermissionConstants.GROUP_STORAGE).toList())
                                .defaultDialog()
                                .request {
                                    Log.d("request", "requestLocationBackground onSuccess")
                                }
                        }
                    }
                }
            }
        }

    }

}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PermissionItem(name: String, onClick: () -> Unit) {
    ListItem(headlineText = {
        Text(text = name)
    }, modifier = Modifier.clickable(onClick = onClick))
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    PermissionTheme {
        Greeting("Android")
    }
}